﻿// <copyright file="King.cs" company="ALCPU">
// Copyright (c) 2010 All Right Reserved
// </copyright>
// <author>Arthur Liberman</author>
// <email>Arthur_Liberman@hotmail.com</email>
// <date>03-30-2010</date>
// <summary>Contains King definition.</summary>

namespace Chess.Figures
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Controls;
    using System.Windows.Media;
    using ChessDefinitions;

    /// <summary>
    /// Representative of the King piece
    /// </summary>
    public class King : ChessPiece
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="King"/> class.
        /// </summary>
        /// <param name="color">The piece color.</param>
        /// <param name="gameboard">The gameboard.</param>
        /// <param name="position">The starting position.</param>
        public King(IPieceColor color, Gameboard gameboard, Position position)
            : base(color, gameboard, position)
        {
            if (color == IPieceColor.Black)
            {
                this.Image = (Brush)App.Current.FindResource("BlackKing");
            }
            else if (color == IPieceColor.White)
            {
                this.Image = (Brush)App.Current.FindResource("WhiteKing");
            }

            this.Gameboard.MovePiece(position, this);
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="King"/> class from being created.
        /// </summary>
        private King()
            : base(0, null, new Position())
        {
        }

        /// <summary>
        /// Fills the move options.
        /// </summary>
        /// <returns>List of moves.</returns>
        protected override List<Position> GetMoveOptions()
        {
            List<Position> posList = new List<Position>();

            if (this.Gameboard.IsSquareFree(new Position(this.Position.X + 1, this.Position.Y)))
            {
                posList.Add(new Position(this.Position.X + 1, this.Position.Y));
            }

            if (this.Gameboard.IsSquareFree(new Position(this.Position.X + 1, this.Position.Y + 1)))
            {
                posList.Add(new Position(this.Position.X + 1, this.Position.Y + 1));
            }

            if (this.Gameboard.IsSquareFree(new Position(this.Position.X + 1, this.Position.Y - 1)))
            {
                posList.Add(new Position(this.Position.X + 1, this.Position.Y - 1));
            }

            if (this.Gameboard.IsSquareFree(new Position(this.Position.X - 1, this.Position.Y)))
            {
                posList.Add(new Position(this.Position.X - 1, this.Position.Y));
            }

            if (this.Gameboard.IsSquareFree(new Position(this.Position.X - 1, this.Position.Y + 1)))
            {
                posList.Add(new Position(this.Position.X - 1, this.Position.Y + 1));
            }

            if (this.Gameboard.IsSquareFree(new Position(this.Position.X - 1, this.Position.Y - 1)))
            {
                posList.Add(new Position(this.Position.X - 1, this.Position.Y - 1));
            }

            if (this.Gameboard.IsSquareFree(new Position(this.Position.X, this.Position.Y - 1)))
            {
                posList.Add(new Position(this.Position.X, this.Position.Y - 1));
            }

            if (this.Gameboard.IsSquareFree(new Position(this.Position.X, this.Position.Y + 1)))
            {
                posList.Add(new Position(this.Position.X, this.Position.Y + 1));
            }

            return posList;
        }

        /// <summary>
        /// Fills the attack list.
        /// </summary>
        /// <returns>List of possible attacks.</returns>
        protected override List<Position> GetAttackList()
        {
            List<Position> posList = new List<Position>();

            if (this.Gameboard.IsSquareAttackable(new Position(this.Position.X + 1, this.Position.Y), this))
            {
                posList.Add(new Position(this.Position.X + 1, this.Position.Y));
            }

            if (this.Gameboard.IsSquareAttackable(new Position(this.Position.X + 1, this.Position.Y + 1), this))
            {
                posList.Add(new Position(this.Position.X + 1, this.Position.Y + 1));
            }

            if (this.Gameboard.IsSquareAttackable(new Position(this.Position.X + 1, this.Position.Y - 1), this))
            {
                posList.Add(new Position(this.Position.X + 1, this.Position.Y - 1));
            }

            if (this.Gameboard.IsSquareAttackable(new Position(this.Position.X - 1, this.Position.Y), this))
            {
                posList.Add(new Position(this.Position.X - 1, this.Position.Y));
            }

            if (this.Gameboard.IsSquareAttackable(new Position(this.Position.X - 1, this.Position.Y + 1), this))
            {
                posList.Add(new Position(this.Position.X - 1, this.Position.Y + 1));
            }

            if (this.Gameboard.IsSquareAttackable(new Position(this.Position.X - 1, this.Position.Y - 1), this))
            {
                posList.Add(new Position(this.Position.X - 1, this.Position.Y - 1));
            }

            if (this.Gameboard.IsSquareAttackable(new Position(this.Position.X, this.Position.Y - 1), this))
            {
                posList.Add(new Position(this.Position.X, this.Position.Y - 1));
            }

            if (this.Gameboard.IsSquareAttackable(new Position(this.Position.X, this.Position.Y + 1), this))
            {
                posList.Add(new Position(this.Position.X, this.Position.Y + 1));
            }

            return posList;
        }
    }
}